package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.list.PcLvSkillList;
import com.lineage.server.datatables.SkillsItemTable;
import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_AddSkill;
import com.lineage.server.serverpackets.S_ItemError;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1SkillItem;
import com.lineage.server.templates.L1Skills;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_SkillBuyItemOK extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_EnterPortal.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport() && 
        !pc.isPrivateShop()) {
        ArrayList<Integer> skillList = null;
        if (pc.isCrown()) {
          skillList = PcLvSkillList.isCrown(pc);
        } else if (pc.isKnight()) {
          skillList = PcLvSkillList.isKnight(pc);
        } else if (pc.isElf()) {
          skillList = PcLvSkillList.isElf(pc);
        } else if (pc.isWizard()) {
          skillList = PcLvSkillList.isWizard(pc);
        } else if (pc.isDarkelf()) {
          skillList = PcLvSkillList.isDarkelf(pc);
        } else if (pc.isDragonKnight()) {
          skillList = PcLvSkillList.isDragonKnight(pc);
        } else if (pc.isIllusionist()) {
          skillList = PcLvSkillList.isIllusionist(pc);
        } 
        if (skillList == null)
          return; 
        boolean isGfx = false;
        int count = readH();
        int i = 0;
        while (i < count) {
          int skillId = readD() + 1;
          if (!CharSkillReading.get().spellCheck(pc.getId(), skillId) && 
            skillList.contains(new Integer(skillId - 1))) {
            L1Skills l1skills = SkillsTable.get().getTemplate(skillId);
            L1SkillItem priceItem = SkillsItemTable.get().getTemplate(skillId);
            if (priceItem != null && priceItem.get_items() != null) {
              int length = (priceItem.get_items()).length;
              boolean[] isOks = new boolean[length];
              int x = 0;
              while (x < length) {
                int itemId = priceItem.get_items()[x];
                int itemCount = priceItem.get_counts()[x];
                if (!pc.getInventory().checkItem(itemId, itemCount)) {
                  isOks[x] = false;
                } else {
                  isOks[x] = true;
                } 
                x++;
              } 
              boolean isShopOk = true;
              boolean[] array;
              int length2 = (array = isOks).length;
              int j = 0;
              while (j < length2) {
                boolean isOk = array[j];
                if (!isOk)
                  isShopOk = false; 
                j++;
              } 
              if (isShopOk) {
                int x2 = 0;
                while (x2 < (priceItem.get_items()).length) {
                  int itemId2 = priceItem.get_items()[x2];
                  int itemCount2 = priceItem.get_counts()[x2];
                  if (pc.getInventory().checkItem(itemId2, itemCount2))
                    pc.getInventory().consumeItem(itemId2, itemCount2); 
                  x2++;
                } 
                CharSkillReading.get().spellMastery(pc.getId(), l1skills.getSkillId(), 
                    l1skills.getName(), 0, 0);
                pc.sendPackets((ServerBasePacket)new S_AddSkill(pc, skillId));
                isGfx = true;
              } else {
                pc.sendPackets((ServerBasePacket)new S_ItemError(skillId - 1));
              } 
            } else {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(939));
              _log.error("购买技能 材料 设置资料异常 材料未设置: " + skillId);
            } 
          } 
          i++;
        } 
        if (isGfx) {
          pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), 224));
          return;
        } 
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
